﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XRpgLibrary.GraphicsClasses
{
    public class GradientTexture2D : Texture2D
    {
        private Color colorStart;
        private Color colorEnd;

        public GradientTexture2D(GraphicsDevice gDevice, int width, int height, Color topColor, Color bottomColor)
            : base (gDevice, width, height, false, SurfaceFormat.Color)
        {
            colorStart = topColor;
            colorEnd = bottomColor;

            Color[] pixels = new Color[width * height];
            for (int y = 0; y < height; y++)
                for (int x = 0; x < width; x++)
                    pixels[y * width + x] = Interpolate(y);

            this.SetData(pixels);
        }

        private Color Interpolate(int step)
        {
            return new Color(
                colorStart.R + ((colorEnd.R - colorStart.R) * step / this.Height),
                colorStart.G + ((colorEnd.G - colorStart.G) * step / this.Height),
                colorStart.B + ((colorEnd.B - colorStart.B) * step / this.Height),
                colorStart.A + ((colorEnd.A - colorStart.A) * step / this.Height)
            );
        }

    }
}
